﻿module Runner {
    export class TextWindow extends Phaser.Group {
        window: Phaser.Graphics;
        windowTextArray: string[];
        windowTextGraphic: Phaser.Text;
        windowLines: number;

        constructor(game: Phaser.Game, name: string, x: number, y: number, width: number, height: number, style: any = { font: '12px verdana', fill: '#cccccc' }) {
            super(game, null, name, true);

            this.buildWindow(x,y,width, height);
            this.buildText(style);
            
        }

        buildWindow(x:number,y:number,width:number,height:number) {
            this.window = this.game.add.graphics(x, y);

            this.window.lineStyle(1, 0x000000, 0.4);
            this.window.beginFill(0xffffff, 0.2);
            this.window.drawRect(0, 0, width, height);
            this.window.endFill();

            this.add(this.window);
        }

        buildText(style: any) {
            this.windowTextArray = [];
            this.windowTextGraphic = this.game.make.text(5, 5, '', style);
            this.windowLines = Math.floor(this.window.height / this.windowTextGraphic.height);

            this.window.addChild(this.windowTextGraphic);
        }

        writeWindowText(text: string): Phaser.Text;
        writeWindowText(text: string): any {
            this.windowTextArray.push(text);
            if (this.windowTextArray.length > this.windowLines) {
                this.windowTextArray.shift();
            }

            this.windowTextGraphic.setText(this.windowTextArray.join('\r\n'));

            return this.windowTextGraphic;
        }
    }
}